<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Article;
use Illuminate\Support\Facades\Redirect;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($var = null)
    {
        $res  = Article::select('id','table','cagetory','originate','page_views','status','updated_at')->take(10)->get()->toArray();

        //方法1
//        dd($res);
//        $data = [
//            'res'=>$res,
//        return view('admin.article.article-list',$data);
//        ];

        //方法二
        return view('admin.article.article-list',compact('res'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
*/
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        //修改启用状态 ajax
        if($request->action &&$request->action == 'is_use_edit'){
            if($request->is_use == '1'){
                $data = [
                    'is_use'=>'2',
                ];
            }else{
                $data = [
                    'is_use'=>'1',
                ];
            }

            $res = Article::where('id','=',$request->id)->update($data);

            if($res){
                return json_encode(['status'=>'SUCCESE','message'=>'ok']);
            }else{
                return json_encode(['status'=>'FAIL','message'=>'error']);
            }
        }

        //搜索框  多条件搜索ajax
        if($request->action &&$request->action == 'article_search'){

//            dd($request->all());
            $res = Article::select('id','table','cagetory','originate','page_views','status','created_at')
                ->when($request->datemin, function ($query) use ($request) {
                    return $query->where('created_at','>=',$request->datemin);
                }, function ($query) {
                    return $query->where('created_at','>=','2018-01-01 00:00:00');
                })
                ->when($request->datemax, function ($query) use ($request) {
                    return $query->where('created_at','<=',$request->datemax);
                }, function ($query) {
                    return $query->where('created_at','<=','2018-12-30 23:59:59');
                })
                ->when($request->search_name, function ($query) use ($request) {
                    return $query->where('table','=',$request->search_name);
                })
                ->get()->toArray();

            return json_encode(['status'=>'SUCCESE','data'=>$res]);

        }

//        dd($request->all());

        if($request->action == 'status'){
            $array = array('msg'=>'添加失败!','status'=>'false');
            if($request->status == '1'){
                $data = [
                    'admin1_status' =>'2',
                ];
                $res = Article::where('id','=',$request->admin1_id)->update($data);
                $array = array('msg'=>'添加成功!','status'=>'success');
            }
            if($request->status == '2'){
                $data = [
                    'admin1_status' => '1',
                ];
                $res = Article::where('id','=',$request->admin1_id)->update($data);
                $array = array('msg'=>'添加成功!','status'=>'success');
            }
            return json_encode($array);
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $res = Article::select('admin1s.role','admin1s.name','admin1s.phone','admin1s.id','admin1s.is_use')->with('admin1_hasone_admin:admins.id,admins.name')->where('admin1s.id','=',$id)->get()->toArray();

        $data = [
            'res'=>$res,
        ];

        return view('admin.admin1.edit',$data);
        //
        dd(unserialize($id));
        return view('admin.admin1.admin-add');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
